package info.cuatrvientos.org;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Update
 * @author pablo_guerrero
 */
@WebServlet("/Update")
public class Update extends HttpServlet {
	String id;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		selectTale(request.getParameter("id"), request);        
        id=request.getParameter("id");
        this.getServletContext().getRequestDispatcher("/update.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		updateTale(request.getParameter("title"),request.getParameter("tale"),id);
        
        this.getServletContext().getRequestDispatcher("/Home").forward(request,response);
	}
	
	private void updateTale(String title, String tale, String id){
        
        String urlcon = "jdbc:mysql://localhost:3306/onceupon";
        
        Connection conn;
        try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(urlcon,"root","");                   
                Statement stat = conn.createStatement();
                stat.executeUpdate("update tale set title='"+title+"',tale='"+tale+"' where id = "+id); 
                
        } catch (SQLException e) {
                // TODO Auto-generated catch blockx
                e.printStackTrace();
        } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }                                       
}

private void selectTale(String id, HttpServletRequest request){                 
        String title = null;
        String tale = null;
        String urlcon = "jdbc:mysql://localhost:3306/onceupon";
        ResultSet resultSet = null;
        Connection conn;
        try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(urlcon,"root","");
                Statement stat = conn.createStatement();
                resultSet = stat.executeQuery("select * from tale where id = "+id);             
                if(resultSet.next()){
                        title = resultSet.getString("title");
                        tale = resultSet.getString("tale");
                        }
                
				}catch (SQLException e) {
				        // TODO Auto-generated catch blockx
				        e.printStackTrace();
				} catch (ClassNotFoundException e) {
				        // TODO Auto-generated catch block
				        e.printStackTrace();
				}
				        request.setAttribute("tale",tale);
				        request.setAttribute("title",title);
				}

}
